package com.lc.schedule.controller;

import com.lc.schedule.common.Result;
import com.lc.schedule.common.ResultCodeEnum;
import com.lc.schedule.pojo.SysUser;
import com.lc.schedule.service.SysUserService;
import com.lc.schedule.service.impl.SysUserServiceImpl;
import com.lc.schedule.util.MD5Util;
import com.lc.schedule.util.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuchun
 */
@WebServlet("/user/*")
public class SysUserController extends BaseController{

    private SysUserService userService = new SysUserServiceImpl();


    //校验用户名是否被占用
    protected void checkUsernameUsed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setCharacterEncoding("UTF-8");
        //得到用户名数据
        String username = request.getParameter("username");
        SysUser loginUser = userService.findByUserName(username);

        Result result = Result.ok(null);

        if (null != loginUser){
            result = Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
//        ObjectMapper objectMapper = new ObjectMapper();
//        String info= objectMapper.writeValueAsString(result);
//        //告诉客户端响应的是一个json串
//        response.setContentType("applicatio/json;charset=UTF-8");
//        response.getWriter().write(info);
        WebUtil.writeJson(response,result);
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收客户端提交的JSON数据
        SysUser sysUser = WebUtil.readJson(request, SysUser.class);
        //String username  = request.getParameter("username");
        //System.out.println(username);
        //String userPwd = request.getParameter("userPwd");

        //调用服务层方法，根据用户名查询用户信息
        SysUser loginUser =  userService.findByUserName(sysUser.getUsername());
        //System.out.println(loginUser);

        Result result = Result.ok(null);
        if (loginUser == null){  //没有查到用户名
           result = Result.build(null,ResultCodeEnum.USERNAME_ERROR);//用户名错误
           WebUtil.writeJson(response,result);
        }else if(!MD5Util.encrypt(sysUser.getUserPwd()).equals(loginUser.getUserPwd())){
            //用户名正确，密码错误
            result = Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
           WebUtil.writeJson(response,result);
        }else {
//            HttpSession session = request.getSession();
//            session.setAttribute("SysUser",loginUser);
//            response.sendRedirect("/showSchedule.html");
            Map data = new HashMap();
            loginUser.setUserPwd("");//设置密码为空，防止密码泄露
            data.put("loginUser",loginUser);
            result = Result.ok(data);
            WebUtil.writeJson(response,result);
        }
    }

    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收客户端提交的参数
//        String username  = request.getParameter("username");
//        String userPwd = request.getParameter("userPwd");
        SysUser loginUser = WebUtil.readJson(request, SysUser.class);
        //将用户名和密码进行封装
//        SysUser user = new SysUser(null,username,userPwd);
        //调用服务层的方法，完成注册功能
        int rows = userService.register(loginUser);
        //能用重定向，就不用请求转发
//        if (rows > 0){
//            response.sendRedirect("/registSuccess.html");
//        }else {
//            response.sendRedirect("/registFail.html");
//        }
        //注册失败
        if (rows > 0){
            Result result = Result.ok(null);
            WebUtil.writeJson(response,result);
        }else {
            Result result = Result.build(null, ResultCodeEnum.USERNAME_USED);
            WebUtil.writeJson(response,result);
        }
    }
}
